﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace BmpComparator
{
    class BmpComparator
    {
        const string dir = "C:\\Users\\Mith\\Documents\\Studia\\MGR\\comparisons\\";
        const string image1 = "out2_noSS.bmp";
        const string image2 = "out2gaus.bmp";
        static FileStream fs;
        static bool saveToFile = true;

        static void Main(string[] args)
        {
            Bitmap bitmap1 = new Bitmap(dir + image1);
            Bitmap bitmap2 = new Bitmap(dir + image2);

            if (bitmap1.Size == bitmap2.Size)
            {
                CompareBitmaps(bitmap1, bitmap2);
            }
            else
            {
                Console.Out.WriteLine("sizes don't match!");
            }
            Console.ReadKey();
        }


        static void CompareBitmaps(Bitmap bitmap1, Bitmap bitmap2)
        {
            double globalDiffAverage = 0.0;
            double globalDiffMin = 444.0;
            int x = 0, y = 0;
            for (int offx = -1; offx < 2; offx++)
            {
                for (int offy = -1; offy < 2; offy++)
                {
                    double diffTotal = 0.0;
                    for (int i = 1; i < bitmap1.Width-1; i++)
                    {
                        for (int j = 1; j < bitmap1.Height-1; j++)
                        {
                            Color color1 = bitmap1.GetPixel(i, j);
                            Color color2 = bitmap2.GetPixel(i+offx, j+offy);
                            diffTotal += GetColorDiff(color1, color2);
                        }
                    }
                    double diffAverage = diffTotal / (bitmap1.Width * bitmap1.Height);
                    Console.Out.WriteLine("offx {0}, offy {1}:\n\tdiffTotal: {2}, diffAverage: {3}", offx, offy, diffTotal, diffAverage);
                    globalDiffAverage += diffAverage;
                    if (diffAverage < globalDiffMin)
                    {
                        globalDiffMin = diffAverage;
                        x = offx;
                        y = offy;
                    }
                    globalDiffMin = globalDiffMin <= diffAverage ? globalDiffMin : diffAverage;
                }
            }
            globalDiffAverage /= 9.0;
            Console.Out.WriteLine("\n{0} --- {1}", image1, image2);
            Console.Out.WriteLine("diffAverage: {0}\ndiffMin: {1} for offset: ({2},{3})", globalDiffAverage, globalDiffMin, x, y);
            if (saveToFile)
            {
                SaveToFile(globalDiffAverage, globalDiffMin, x, y);
            }
        }


        private static void SaveToFile(double globalDiffAverage, double globalDiffMin, int x, int y)
        {
            fs = File.Open(dir + "results.txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("{0} --- {1}\r\n", image1, image2);
            sw.Write("diffAverage: {0}\r\ndiffMin: {1} for offset: ({2},{3})\r\n\r\n", globalDiffAverage, globalDiffMin, x, y);
            sw.Close();
            fs.Close();
        }
        

        static double GetColorDiff(Color color1, Color color2)
        {
            double diff = 0.0;
            diff += Math.Pow(Math.Abs(color1.R - color2.R), 2.0);
            diff += Math.Pow(Math.Abs(color1.G - color2.G), 2.0);
            diff += Math.Pow(Math.Abs(color1.B - color2.B), 2.0);
            return Math.Sqrt(diff);
        }
    }
}
